package com.collins.thread.practices.ticket;

import java.util.Scanner;

public class SellTicket {
    public static void main(String[] args) {
        System.out.println("please input mode:");
        System.out.println("1 for lock; 2 for synchronized");
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        try {
            i = Integer.parseInt(scanner.nextLine());
        } catch (Exception e) {
            System.err.println("please input a number");
            System.exit(1);
        }
        switch (i) {
            case 1:
                sellLock();
                break;
            case 2:
                sellSynchronized();
                break;
            default:
                System.out.println("wrong input ... try again");
                System.exit(0);
        }


    }

    public static void sellLock() {
        TicketLockRunnable sl = new TicketLockRunnable();
        Thread t1 = new Thread(sl);
        Thread t2 = new Thread(sl);
        Thread t3 = new Thread(sl);

        t1.setName("booth1");
        t2.setName("booth2");
        t3.setName("booth3");

        t1.start();
        t2.start();
        t3.start();
    }

    public static void sellSynchronized() {
        TicketSynchonizedRunnable st = new TicketSynchonizedRunnable();
        Thread t1 = new Thread(st);
        Thread t2 = new Thread(st);
        Thread t3 = new Thread(st);

        t1.setName("booth1");
        t2.setName("booth2");
        t3.setName("booth3");

        t1.start();
        t2.start();
        t3.start();
    }
}
